package com.carlos.camunda.config;

import ch.qos.logback.core.db.dialect.MySQLDialect;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.ProcessEngines;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.support.JdbcTransactionManager;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author 李健成
 * @date 2021/11/22
 */

@Configuration
public class CamundaConfig {

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "datasource.primary")
    public DataSource primaryDataSource() throws ClassNotFoundException {
        return DataSourceBuilder.create()
                .driverClassName("com.mysql.cj.jdbc.Driver")
                .url("jdbc:mysql://localhost:3306/camunda")
                .username("root")
                .password("root123456")
                .type((Class<DataSource>) Class.forName("com.mysql.cj.jdbc.MysqlDataSource"))
                .build();
    }

    @Bean
    @Primary
    public PlatformTransactionManager primaryTransactionManager() throws ClassNotFoundException {
        return new DataSourceTransactionManager(primaryDataSource());
    }

    @Bean
    public ProcessEngine processEngine() {
        return ProcessEngines.getDefaultProcessEngine();
    }

}
